<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
  Class: Mocha::Expectation
  
    &mdash; Mocha 3.0.0-rc.2
  
</title>

  <link rel="stylesheet" href="../css/style.css" type="text/css" />

  <link rel="stylesheet" href="../css/common.css" type="text/css" />

<script type="text/javascript">
  pathId = "Mocha::Expectation";
  relpath = '../';
</script>


  <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>


  </head>
  <body>
    <div class="nav_wrap">
      <iframe id="nav" src="../class_list.html?1"></iframe>
      <div id="resizer"></div>
    </div>

    <div id="main" tabindex="-1">
      <div id="header">
        <div id="menu">
  
    <a href="../_index.html">Index (E)</a> &raquo;
    <span class='title'><span class='object_link'><a href="../Mocha.html" title="Mocha (module)">Mocha</a></span></span>
     &raquo; 
    <span class="title">Expectation</span>
  
</div>

        <div id="search">
  
    <a class="full_list_link" id="class_list_link"
        href="../class_list.html">

        <svg width="24" height="24">
          <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
          <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
          <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
        </svg>
    </a>
  
</div>
        <div class="clear"></div>
      </div>

      <div id="content"><h1>Class: Mocha::Expectation
  
  
  
</h1>
<div class="box_info">
  
  <dl>
    <dt>Inherits:</dt>
    <dd>
      <span class="inheritName"><span class='object_link'>Object</span></span>
      
        <ul class="fullTree">
          <li><span class='object_link'>Object</span></li>
          
            <li class="next">Mocha::Expectation</li>
          
        </ul>
        <a href="#" class="inheritanceTree">show all</a>
      
    </dd>
  </dl>
  

  
  
  
  
  

  

  
  <dl>
    <dt>Defined in:</dt>
    <dd>lib/mocha/expectation.rb</dd>
  </dl>
  
</div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    
<p>Methods on expectations returned from <span class='object_link'><a href="Mock.html#expects-instance_method" title="Mocha::Mock#expects (method)">Mock#expects</a></span>, <span class='object_link'><a href="Mock.html#stubs-instance_method" title="Mocha::Mock#stubs (method)">Mock#stubs</a></span>, <span class='object_link'><a href="ObjectMethods.html#expects-instance_method" title="Mocha::ObjectMethods#expects (method)">ObjectMethods#expects</a></span> and <span class='object_link'><a href="ObjectMethods.html#stubs-instance_method" title="Mocha::ObjectMethods#stubs (method)">ObjectMethods#stubs</a></span>.</p>


  </div>
</div>
<div class="tags">
  

</div>






  
    <h2>
      Instance Method Summary
      <small><a href="#" class="summary_toggle">collapse</a></small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#at_least-instance_method" title="#at_least (instance method)">#<strong>at_least</strong>(minimum_number_of_times)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called at least a <code>minimum_number_of_times</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#at_least_once-instance_method" title="#at_least_once (instance method)">#<strong>at_least_once</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called at least once.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#at_most-instance_method" title="#at_most (instance method)">#<strong>at_most</strong>(maximum_number_of_times)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called at most a <code>maximum_number_of_times</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#at_most_once-instance_method" title="#at_most_once (instance method)">#<strong>at_most_once</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called at most once.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#in_sequence-instance_method" title="#in_sequence (instance method)">#<strong>in_sequence</strong>(sequence, *sequences)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Constrains the expectation so that it must be invoked at the current point in the <code>sequence</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#multiple_yields-instance_method" title="#multiple_yields (instance method)">#<strong>multiple_yields</strong>(*parameter_groups)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified <code>parameter_groups</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#never-instance_method" title="#never (instance method)">#<strong>never</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must never be called.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#once-instance_method" title="#once (instance method)">#<strong>once</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called exactly once.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#raises-instance_method" title="#raises (instance method)">#<strong>raises</strong>(exception = RuntimeError, message = nil)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that when the expected method is called, it raises the specified <code>exception</code> with the specified <code>message</code> i.e.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#returns-instance_method" title="#returns (instance method)">#<strong>returns</strong>(*values)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that when the expected method is called, it returns the specified <code>value</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#then-instance_method" title="#then (instance method)">#<strong>then</strong>(state = nil)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>The same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span> methods to be chained.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#thrice-instance_method" title="#thrice (instance method)">#<strong>thrice</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called exactly three times.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#throws-instance_method" title="#throws (instance method)">#<strong>throws</strong>(tag, object = nil)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that when the expected method is called, it throws the specified <code>tag</code> with the specific return value <code>object</code> i.e.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#times-instance_method" title="#times (instance method)">#<strong>times</strong>(range_or_number)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the number of invocations of the expected method must be within a specified range or exactly equal to a specified number.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#twice-instance_method" title="#twice (instance method)">#<strong>twice</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called exactly twice.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#when-instance_method" title="#when (instance method)">#<strong>when</strong>(state_predicate)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Constrains the expectation to occur only when the <code>state_machine</code> is in the state specified by <code>state_predicate</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#with-instance_method" title="#with (instance method)">#<strong>with</strong>(*expected_parameters_or_matchers) {|actual_parameters| ... } &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called with <code>expected_parameters_or_matchers</code>.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#with_block_given-instance_method" title="#with_block_given (instance method)">#<strong>with_block_given</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called with a block.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#with_no_block_given-instance_method" title="#with_no_block_given (instance method)">#<strong>with_no_block_given</strong>  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that the expected method must be called without a block.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#yields-instance_method" title="#yields (instance method)">#<strong>yields</strong>(*parameters)  &#x21d2; Expectation </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>Modifies expectation so that when the expected method is called, it yields to the block with the specified <code>parameters</code>.</p>
</div></span>
  
</li>

      
    </ul>
  



  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>

    
      <div class="method_details first">
  <h3 class="signature first" id="at_least-instance_method">
  
    #<strong>at_least</strong>(minimum_number_of_times)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called at least a <code>minimum_number_of_times</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called at least twice.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_least'>at_least</span><span class='lparen'>(</span><span class='int'>2</span><span class='rparen'>)</span>
<span class='int'>3</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_least'>at_least</span><span class='lparen'>(</span><span class='int'>2</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>minimum_number_of_times</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>minimum number of expected invocations.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


164
165
166
167</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 164</span>

<span class='kw'>def</span> <span class='id identifier rubyid_at_least'>at_least</span><span class='lparen'>(</span><span class='id identifier rubyid_minimum_number_of_times'>minimum_number_of_times</span><span class='rparen'>)</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_at_least'>at_least</span><span class='lparen'>(</span><span class='id identifier rubyid_minimum_number_of_times'>minimum_number_of_times</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="at_least_once-instance_method">
  
    #<strong>at_least_once</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called at least once. This is equivalent to calling <span class='object_link'><a href="#at_least-instance_method" title="Mocha::Expectation#at_least (method)">#at_least</a></span> with an argument of <code>1</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called at least once.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_least_once'>at_least_once</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_least_once'>at_least_once</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


182
183
184</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 182</span>

<span class='kw'>def</span> <span class='id identifier rubyid_at_least_once'>at_least_once</span>
  <span class='id identifier rubyid_at_least'>at_least</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="at_most-instance_method">
  
    #<strong>at_most</strong>(maximum_number_of_times)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called at most a <code>maximum_number_of_times</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called at most twice.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_most'>at_most</span><span class='lparen'>(</span><span class='int'>2</span><span class='rparen'>)</span>
<span class='int'>2</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_most'>at_most</span><span class='lparen'>(</span><span class='int'>2</span><span class='rparen'>)</span>
<span class='int'>3</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span> <span class='comment'># =&gt; unexpected invocation</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>maximum_number_of_times</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>maximum number of expected invocations.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


200
201
202
203</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 200</span>

<span class='kw'>def</span> <span class='id identifier rubyid_at_most'>at_most</span><span class='lparen'>(</span><span class='id identifier rubyid_maximum_number_of_times'>maximum_number_of_times</span><span class='rparen'>)</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_at_most'>at_most</span><span class='lparen'>(</span><span class='id identifier rubyid_maximum_number_of_times'>maximum_number_of_times</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="at_most_once-instance_method">
  
    #<strong>at_most_once</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called at most once. This is equivalent to calling <span class='object_link'><a href="#at_most-instance_method" title="Mocha::Expectation#at_most (method)">#at_most</a></span> with an argument of <code>1</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called at most once.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_most_once'>at_most_once</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_at_most_once'>at_most_once</span>
<span class='int'>2</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span> <span class='comment'># =&gt; unexpected invocation</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


218
219
220</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 218</span>

<span class='kw'>def</span> <span class='id identifier rubyid_at_most_once'>at_most_once</span>
  <span class='id identifier rubyid_at_most'>at_most</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="in_sequence-instance_method">
  
    #<strong>in_sequence</strong>(sequence, *sequences)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Constrains the expectation so that it must be invoked at the current point in the <code>sequence</code>.</p>

<p>To expect a sequence of invocations, write the expectations in order and add the <tt>in_sequence(sequence)</tt> clause to each one.</p>

<p>Expectations in a <code>sequence</code> can have any invocation count.</p>

<p>If an expectation in a sequence is stubbed, rather than expected, it can be skipped in the <code>sequence</code>.</p>

<p>An expected method can appear in multiple sequences.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Ensure methods are invoked in a specified order.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_breakfast'>breakfast</span> <span class='op'>=</span> <span class='id identifier rubyid_sequence'>sequence</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>breakfast</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>

<span class='id identifier rubyid_egg'>egg</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>egg</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_egg'>egg</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:crack</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_in_sequence'>in_sequence</span><span class='lparen'>(</span><span class='id identifier rubyid_breakfast'>breakfast</span><span class='rparen'>)</span>
<span class='id identifier rubyid_egg'>egg</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:fry</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_in_sequence'>in_sequence</span><span class='lparen'>(</span><span class='id identifier rubyid_breakfast'>breakfast</span><span class='rparen'>)</span>
<span class='id identifier rubyid_egg'>egg</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:eat</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_in_sequence'>in_sequence</span><span class='lparen'>(</span><span class='id identifier rubyid_breakfast'>breakfast</span><span class='rparen'>)</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>sequence</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="Sequence.html" title="Mocha::Sequence (class)">Sequence</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>sequence in which expected method should appear.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>sequences</span>
      
      
        <span class='type'>(<tt>*Array&lt;<span class='object_link'><a href="Sequence.html" title="Mocha::Sequence (class)">Sequence</a></span>&gt;</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>more sequences in which expected method should appear.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="API.html#sequence-instance_method" title="Mocha::API#sequence (method)">API#sequence</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


643
644
645
646</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 643</span>

<span class='kw'>def</span> <span class='id identifier rubyid_in_sequence'>in_sequence</span><span class='lparen'>(</span><span class='id identifier rubyid_sequence'>sequence</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_sequences'>sequences</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_sequences'>sequences</span><span class='period'>.</span><span class='id identifier rubyid_unshift'>unshift</span><span class='lparen'>(</span><span class='id identifier rubyid_sequence'>sequence</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_seq'>seq</span><span class='op'>|</span> <span class='id identifier rubyid_add_in_sequence_ordering_constraint'>add_in_sequence_ordering_constraint</span><span class='lparen'>(</span><span class='id identifier rubyid_seq'>seq</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="multiple_yields-instance_method">
  
    #<strong>multiple_yields</strong>(*parameter_groups)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified <code>parameter_groups</code>.</p>

<p>If no block is provided, the method will still attempt to yield resulting in a <code>LocalJumpError</code>. Note that this is what would happen if a “real” (non-mock) method implementation tried to yield to a non-existent block.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>When <code>foreach</code> is called, the stub will invoke the block twice, the first time it passes [‘row1_col1’, ‘row1_col2’] as the parameters, and the second time it passes [‘row2_col1’, ”] as the parameters.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_csv'>csv</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_csv'>csv</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foreach</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>path/to/file.csv</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_multiple_yields'>multiple_yields</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>row1_col1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>row1_col2</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>row2_col1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='rparen'>)</span>
<span class='id identifier rubyid_rows'>rows</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_csv'>csv</span><span class='period'>.</span><span class='id identifier rubyid_foreach'>foreach</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_row'>row</span><span class='op'>|</span> <span class='id identifier rubyid_rows'>rows</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_row'>row</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_rows'>rows</span> <span class='comment'># =&gt; [[&#39;row1_col1&#39;, &#39;row1_col2&#39;], [&#39;row2_col1&#39;, &#39;&#39;]]</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Yield different groups of parameters on different invocations of the expected method. Simulating a situation where the CSV file at ‘path/to/file.csv’ has been modified between the two calls to <code>foreach</code>.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_csv'>csv</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_csv'>csv</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:foreach</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>path/to/file.csv</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_multiple_yields'>multiple_yields</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>old_row1_col1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>old_row1_col2</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>old_row2_col1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_multiple_yields'>multiple_yields</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>new_row1_col1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>new_row2_col1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>new_row2_col2</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='rparen'>)</span>
<span class='id identifier rubyid_rows_from_first_invocation'>rows_from_first_invocation</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_rows_from_second_invocation'>rows_from_second_invocation</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_csv'>csv</span><span class='period'>.</span><span class='id identifier rubyid_foreach'>foreach</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_row'>row</span><span class='op'>|</span> <span class='id identifier rubyid_rows_from_first_invocation'>rows_from_first_invocation</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_row'>row</span> <span class='rbrace'>}</span> <span class='comment'># first invocation
</span><span class='id identifier rubyid_csv'>csv</span><span class='period'>.</span><span class='id identifier rubyid_foreach'>foreach</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_row'>row</span><span class='op'>|</span> <span class='id identifier rubyid_rows_from_second_invocation'>rows_from_second_invocation</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_row'>row</span> <span class='rbrace'>}</span> <span class='comment'># second invocation
</span><span class='id identifier rubyid_rows_from_first_invocation'>rows_from_first_invocation</span> <span class='comment'># =&gt; [[&#39;old_row1_col1&#39;, &#39;old_row1_col2&#39;], [&#39;old_row2_col1&#39;, &#39;&#39;]]
</span><span class='id identifier rubyid_rows_from_second_invocation'>rows_from_second_invocation</span> <span class='comment'># =&gt; [[&#39;new_row1_col1&#39;, &#39;&#39;], [&#39;new_row2_col1&#39;, &#39;new_row2_col2&#39;]]</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>parameter_groups</span>
      
      
        <span class='type'>(<tt>*Array&lt;Array&gt;</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>each element of <code>parameter_groups</code> should iself be an <code>Array</code> representing the parameters to be passed to the block for a single yield. Any element of <code>parameter_groups</code> that is not an <code>Array</code> is wrapped in an <code>Array</code>.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


430
431
432
433</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 430</span>

<span class='kw'>def</span> <span class='id identifier rubyid_multiple_yields'>multiple_yields</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_parameter_groups'>parameter_groups</span><span class='rparen'>)</span>
  <span class='ivar'>@yield_parameters</span><span class='period'>.</span><span class='id identifier rubyid_add'>add</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_parameter_groups'>parameter_groups</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="never-instance_method">
  
    #<strong>never</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must never be called.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must never be called.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_never'>never</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; unexpected invocation
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_never'>never</span>
<span class='comment'># =&gt; verify succeeds</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


144
145
146
147</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 144</span>

<span class='kw'>def</span> <span class='id identifier rubyid_never'>never</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_exactly'>exactly</span><span class='lparen'>(</span><span class='int'>0</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="once-instance_method">
  
    #<strong>once</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called exactly once. This is equivalent to calling <span class='object_link'><a href="#times-instance_method" title="Mocha::Expectation#times (method)">#times</a></span> with an argument of <code>1</code>.</p>

<p>Note that this is the default behaviour for an expectation, but you may wish to use it for clarity/emphasis.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be invoked exactly once.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_once'>once</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_once'>once</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; unexpected invocation
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_once'>once</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


127
128
129
130</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 127</span>

<span class='kw'>def</span> <span class='id identifier rubyid_once'>once</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_exactly'>exactly</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="raises-instance_method">
  
    
      <span class="overload">#<strong>raises</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>raises</strong>(exception)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>raises</strong>(exception, message)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that when the expected method is called, it raises the specified <code>exception</code> with the specified <code>message</code> i.e. calls <tt>Kernel#raise(exception, message)</tt>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Raise specified exception if expected method is invoked.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>message</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception and with message &#39;message&#39;</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Raise custom exception with extra constructor parameters by passing in an instance of the exception.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>MyException</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>message</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises the specified instance of MyException</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Raise different exceptions on consecutive invocations of the expected method.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception1</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception2</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception2</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Raise an exception on first invocation of expected method and then return values on subsequent invocations.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 2
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 3</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>exception</span>
      
      
        <span class='type'>(<tt>Class</tt>, <tt>Exception</tt>, <tt>String</tt>, <tt>#exception</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>RuntimeError</tt>)</em>
      
      
        &mdash;
        <div class='inline'>
<p>exception to be raised or message to be passed to RuntimeError.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>message</span>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>nil</tt>)</em>
      
      
        &mdash;
        <div class='inline'>
<p>exception message.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li>Kernel#raise</li>
    
      <li><span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


517
518
519
520</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 517</span>

<span class='kw'>def</span> <span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='id identifier rubyid_exception'>exception</span> <span class='op'>=</span> <span class='const'>RuntimeError</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='ivar'>@return_values</span> <span class='op'>+=</span> <span class='const'>ReturnValues</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='const'>ExceptionRaiser</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_exception'>exception</span><span class='comma'>,</span> <span class='id identifier rubyid_message'>message</span><span class='rparen'>)</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="returns-instance_method">
  
    
      <span class="overload">#<strong>returns</strong>(value)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>returns</strong>(*values)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that when the expected method is called, it returns the specified <code>value</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Return the same value on every invocation.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>result</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span> <span class='comment'># =&gt; &#39;result&#39;
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span> <span class='comment'># =&gt; &#39;result&#39;</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Return a different value on consecutive invocations.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:stubbed_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubbed_method'>stubbed_method</span> <span class='comment'># =&gt; 2</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Alternative way to return a different value on consecutive invocations.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>3</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 2
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 3</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>May be called in conjunction with <span class='object_link'><a href="#raises-instance_method" title="Mocha::Expectation#raises (method)">#raises</a></span> on the same expectation.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 2
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception1</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Note that in Ruby a method returning multiple values is exactly equivalent to a method returning an <code>Array</code> of those values.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rbracket'>]</span><span class='rparen'>)</span>
<span class='id identifier rubyid_x'>x</span><span class='comma'>,</span> <span class='id identifier rubyid_y'>y</span> <span class='op'>=</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_x'>x</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_y'>y</span> <span class='comment'># =&gt; 2</span></code></pre>
    
  </div>

  <p class="tag_title">Overloads:</p>
  <ul class="overload">
    
      
      <li class="overload_item">
        <span class="signature">#<strong>returns</strong>(value)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>value</span>
      
      
        <span class='type'>(<tt><span class='object_link'>Object</span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>value to return on invocation of expected method.</p>
</div>
      
    </li>
  
</ul>


</div>
      </li>
    
      
      <li class="overload_item">
        <span class="signature">#<strong>returns</strong>(*values)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>values</span>
      
      
        <span class='type'>(<tt>*Array</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>values to return on consecutive invocations of expected method.</p>
</div>
      
    </li>
  
</ul>


</div>
      </li>
    
  </ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


477
478
479
480</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 477</span>

<span class='kw'>def</span> <span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_values'>values</span><span class='rparen'>)</span>
  <span class='ivar'>@return_values</span> <span class='op'>+=</span> <span class='const'>ReturnValues</span><span class='period'>.</span><span class='id identifier rubyid_build'>build</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_values'>values</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="then-instance_method">
  
    
      <span class="overload">#<strong>then</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>then</strong>(state)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Returns the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using <span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span> as syntactic sugar when specifying values to be returned and exceptions to be raised on consecutive invocations of the expected method.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_raises'>raises</span><span class='lparen'>(</span><span class='const'>Exception</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>4</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 2
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; raises exception of class Exception
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 4</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using <span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span> to change the <code>state</code> of a <code>state_machine</code> on the invocation of an expected method.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_power'>power</span> <span class='op'>=</span> <span class='id identifier rubyid_states'>states</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>power</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_starts_as'>starts_as</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>off</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>

<span class='id identifier rubyid_radio'>radio</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>radio</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:switch_on</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:select_channel</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>BBC Radio 4</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:adjust_volume</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='int'>+5</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:select_channel</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>BBC World Service</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:adjust_volume</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='op'>-</span><span class='int'>5</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:switch_off</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>off</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span></code></pre>
    
  </div>

  <p class="tag_title">Overloads:</p>
  <ul class="overload">
    
      
      <li class="overload_item">
        <span class="signature">#<strong>then</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    
<p>Used as syntactic sugar to improve readability. It has no effect on state of the expectation.</p>


  </div>
</div>
<div class="tags">
  

</div>
      </li>
    
      
      <li class="overload_item">
        <span class="signature">#<strong>then</strong>(state)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
        <div class="docstring">
  <div class="discussion">
    
<p>Used to change the <code>state_machine</code> to the specified state when the expected invocation occurs.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>state</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="StateMachine/State.html" title="Mocha::StateMachine::State (class)">StateMachine::State</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>state_machine.is(state_name) provides a mechanism to change the <code>state_machine</code> into the state specified by <code>state_name</code> when the expected method is invoked.</p>
</div>
      
    </li>
  
</ul>


  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="API.html#states-instance_method" title="Mocha::API#states (method)">API#states</a></span></li>
    
      <li><span class='object_link'><a href="StateMachine.html" title="Mocha::StateMachine (class)">StateMachine</a></span></li>
    
      <li><span class='object_link'><a href="#when-instance_method" title="Mocha::Expectation#when (method)">#when</a></span></li>
    
  </ul>

</div>
      </li>
    
  </ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


591
592
593
594</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 591</span>

<span class='kw'>def</span> <span class='kw'>then</span><span class='lparen'>(</span><span class='id identifier rubyid_state'>state</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_add_side_effect'>add_side_effect</span><span class='lparen'>(</span><span class='const'>ChangeStateSideEffect</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_state'>state</span><span class='rparen'>)</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_state'>state</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="thrice-instance_method">
  
    #<strong>thrice</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called exactly three times. This is equivalent to calling <span class='object_link'><a href="#times-instance_method" title="Mocha::Expectation#times (method)">#times</a></span> with an argument of <code>3</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be invoked exactly three times.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_thrice'>thrice</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_thrice'>thrice</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; unexpected invocation
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_thrice'>thrice</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


76
77
78
79</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 76</span>

<span class='kw'>def</span> <span class='id identifier rubyid_thrice'>thrice</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_exactly'>exactly</span><span class='lparen'>(</span><span class='int'>3</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="throws-instance_method">
  
    
      <span class="overload">#<strong>throw</strong>(tag)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
      <span class="overload">#<strong>throw</strong>(tag, object)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> </span>
    
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that when the expected method is called, it throws the specified <code>tag</code> with the specific return value <code>object</code> i.e. calls <tt>Kernel#throw(tag, object)</tt>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Throw tag when expected method is invoked.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_throws'>throws</span><span class='lparen'>(</span><span class='symbol'>:done</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; throws tag :done</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Throw tag with return value <code>object</code> c.f. <tt>Kernel#throw</tt>.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_throws'>throws</span><span class='lparen'>(</span><span class='symbol'>:done</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>result</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; throws tag :done and causes catch block to return &#39;result&#39;</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Throw different tags on consecutive invocations of the expected method.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_throws'>throws</span><span class='lparen'>(</span><span class='symbol'>:done</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_throws'>throws</span><span class='lparen'>(</span><span class='symbol'>:continue</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; throws :done
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; throws :continue</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Throw tag on first invocation of expected method and then return values for subsequent invocations.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_stub'>stub</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_stubs'>stubs</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_throws'>throws</span><span class='lparen'>(</span><span class='symbol'>:done</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_returns'>returns</span><span class='lparen'>(</span><span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; throws :done
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 2
</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; 3</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>tag</span>
      
      
        <span class='type'>(<tt>Symbol</tt>, <tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>tag to throw to transfer control to the active catch block.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>object</span>
      
      
        <span class='type'>(<tt><span class='object_link'>Object</span></tt>)</span>
      
      
        <em class="default">(defaults to: <tt>nil</tt>)</em>
      
      
        &mdash;
        <div class='inline'>
<p>return value for the catch block.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li>Kernel#throw</li>
    
      <li><span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


556
557
558
559</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 556</span>

<span class='kw'>def</span> <span class='id identifier rubyid_throws'>throws</span><span class='lparen'>(</span><span class='id identifier rubyid_tag'>tag</span><span class='comma'>,</span> <span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='ivar'>@return_values</span> <span class='op'>+=</span> <span class='const'>ReturnValues</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='const'>Thrower</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_tag'>tag</span><span class='comma'>,</span> <span class='id identifier rubyid_object'>object</span><span class='rparen'>)</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="times-instance_method">
  
    #<strong>times</strong>(range_or_number)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the number of invocations of the expected method must be within a specified range or exactly equal to a specified number.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Specifying an exact number of expected invocations.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span><span class='lparen'>(</span><span class='int'>3</span><span class='rparen'>)</span>
<span class='int'>3</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span><span class='lparen'>(</span><span class='int'>3</span><span class='rparen'>)</span>
<span class='int'>2</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Specifying a range for the number of expected invocations.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span><span class='lparen'>(</span><span class='int'>2</span><span class='op'>..</span><span class='int'>4</span><span class='rparen'>)</span>
<span class='int'>3</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span><span class='lparen'>(</span><span class='int'>2</span><span class='op'>..</span><span class='int'>4</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>range_or_number</span>
      
      
        <span class='type'>(<tt>Range</tt>, <tt>Integer</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>specifies the allowable range for the number of expected invocations or the specified number of expected invocations.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


47
48
49
50</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 47</span>

<span class='kw'>def</span> <span class='id identifier rubyid_times'>times</span><span class='lparen'>(</span><span class='id identifier rubyid_range_or_number'>range_or_number</span><span class='rparen'>)</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span><span class='lparen'>(</span><span class='id identifier rubyid_range_or_number'>range_or_number</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="twice-instance_method">
  
    #<strong>twice</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called exactly twice. This is equivalent to calling <span class='object_link'><a href="#times-instance_method" title="Mocha::Expectation#times (method)">#times</a></span> with an argument of <code>2</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be invoked exactly twice.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_twice'>twice</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_twice'>twice</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='comment'># =&gt; unexpected invocation
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_twice'>twice</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


102
103
104
105</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 102</span>

<span class='kw'>def</span> <span class='id identifier rubyid_twice'>twice</span>
  <span class='ivar'>@cardinality</span><span class='period'>.</span><span class='id identifier rubyid_exactly'>exactly</span><span class='lparen'>(</span><span class='int'>2</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="when-instance_method">
  
    #<strong>when</strong>(state_predicate)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Constrains the expectation to occur only when the <code>state_machine</code> is in the state specified by <code>state_predicate</code>.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using <span class='object_link'><a href="#when-instance_method" title="Mocha::Expectation#when (method)">#when</a></span> to only allow invocation of methods when “power” state machine is in the “on” state.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_power'>power</span> <span class='op'>=</span> <span class='id identifier rubyid_states'>states</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>power</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_starts_as'>starts_as</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>off</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>

<span class='id identifier rubyid_radio'>radio</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>radio</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:switch_on</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:select_channel</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>BBC Radio 4</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:adjust_volume</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='int'>+5</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:select_channel</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>BBC World Service</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:adjust_volume</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='op'>-</span><span class='int'>5</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_when'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>on</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_radio'>radio</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:switch_off</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='lparen'>(</span><span class='id identifier rubyid_power'>power</span><span class='period'>.</span><span class='id identifier rubyid_is'>is</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>off</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='rparen'>)</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>state_predicate</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="StateMachine/StatePredicate.html" title="Mocha::StateMachine::StatePredicate (class)">StateMachine::StatePredicate</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p><tt>state_machine.is(state_name)</tt> provides a mechanism to determine whether the <code>state_machine</code> is in the state specified by <code>state_predicate</code> when the expected method is invoked.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="API.html#states-instance_method" title="Mocha::API#states (method)">API#states</a></span></li>
    
      <li><span class='object_link'><a href="StateMachine.html" title="Mocha::StateMachine (class)">StateMachine</a></span></li>
    
      <li><span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


615
616
617
618</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 615</span>

<span class='kw'>def</span> <span class='kw'>when</span><span class='lparen'>(</span><span class='id identifier rubyid_state_predicate'>state_predicate</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_add_ordering_constraint'>add_ordering_constraint</span><span class='lparen'>(</span><span class='const'>InStateOrderingConstraint</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_state_predicate'>state_predicate</span><span class='rparen'>)</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="with-instance_method">
  
    #<strong>with</strong>(*expected_parameters_or_matchers) {|actual_parameters| ... } &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called with <code>expected_parameters_or_matchers</code>.</p>

<p>May be used with Ruby literals or variables for exact matching or with parameter matchers for less-specific matching, e.g. <span class='object_link'><a href="ParameterMatchers/Methods.html#includes-instance_method" title="Mocha::ParameterMatchers::Methods#includes (method)">ParameterMatchers::Methods#includes</a></span>, <span class='object_link'><a href="ParameterMatchers/Methods.html#has_key-instance_method" title="Mocha::ParameterMatchers::Methods#has_key (method)">ParameterMatchers::Methods#has_key</a></span>, etc. See <span class='object_link'><a href="ParameterMatchers.html" title="Mocha::ParameterMatchers (module)">ParameterMatchers</a></span> for a list of all available parameter matchers.</p>

<p>Alternatively a block argument can be passed to <span class='object_link'><a href="#with-instance_method" title="Mocha::Expectation#with (method)">#with</a></span> to implement custom parameter matching. The block receives the <tt>*actual_parameters</tt> as its arguments and should return <code>true</code> if they are acceptable or <code>false</code> otherwise. See the example below where a method is expected to be called with a value divisible by 4. The block argument takes precedence over <code>expected_parameters_or_matchers</code>. The block may be called multiple times per invocation of the expected method and so it should be idempotent.</p>

<p>Note that if <span class='object_link'><a href="#with-instance_method" title="Mocha::Expectation#with (method)">#with</a></span> is called multiple times on the same expectation, the last call takes precedence; other calls are ignored.</p>

<p>Positional arguments were separated from keyword arguments in Ruby v3 (see <a href="https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0" target="_parent" title="this article">this article</a>). In relation to this a new configuration option (<span class='object_link'><a href="Configuration.html#strict_keyword_argument_matching=-instance_method" title="Mocha::Configuration#strict_keyword_argument_matching= (method)">Configuration#strict_keyword_argument_matching=</a></span>) is available in Ruby &gt;= 2.7.</p>

<p>When <span class='object_link'><a href="Configuration.html#strict_keyword_argument_matching=-instance_method" title="Mocha::Configuration#strict_keyword_argument_matching= (method)">Configuration#strict_keyword_argument_matching=</a></span> is set to <code>false</code> (which is the default in Ruby v2.7), a positional <code>Hash</code> and a set of keyword arguments passed to <span class='object_link'><a href="#with-instance_method" title="Mocha::Expectation#with (method)">#with</a></span> are treated the same for the purposes of parameter matching. However, a deprecation warning will be displayed if a positional <code>Hash</code> matches a set of keyword arguments or vice versa.</p>

<p>When <span class='object_link'><a href="Configuration.html#strict_keyword_argument_matching=-instance_method" title="Mocha::Configuration#strict_keyword_argument_matching= (method)">Configuration#strict_keyword_argument_matching=</a></span> is set to <code>true</code> (which is the default in Ruby &gt;= v3.0), an actual positional <code>Hash</code> will not match an expected set of keyword arguments; and vice versa, an actual set of keyword arguments will not match an expected positional <code>Hash</code>, i.e. the parameter matching is stricter.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called with exact parameter values.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='symbol'>:param1</span><span class='comma'>,</span> <span class='symbol'>:param2</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='symbol'>:param1</span><span class='comma'>,</span> <span class='symbol'>:param2</span><span class='rparen'>)</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='symbol'>:param1</span><span class='comma'>,</span> <span class='symbol'>:param2</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='symbol'>:param3</span><span class='rparen'>)</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called with parameters matching parameter matchers.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='id identifier rubyid_includes'>includes</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>string2</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='comma'>,</span> <span class='id identifier rubyid_anything'>anything</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>string1</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>string2</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>any-old-value</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='id identifier rubyid_includes'>includes</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>string2</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span><span class='comma'>,</span> <span class='id identifier rubyid_anything'>anything</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>string1</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>any-old-value</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Loose keyword argument matching (default in Ruby v2.7).</p>
</div></h5>
      
      <pre class="example code"><code>
<span class='const'><span class='object_link'><a href="../Mocha.html" title="Mocha (module)">Mocha</a></span></span><span class='period'>.</span><span class='id identifier rubyid_configure'><span class='object_link'><a href="../Mocha.html#configure-class_method" title="Mocha.configure (method)">configure</a></span></span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_c'>c</span><span class='op'>|</span>
  <span class='id identifier rubyid_c'>c</span><span class='period'>.</span><span class='id identifier rubyid_strict_keyword_argument_matching'>strict_keyword_argument_matching</span> <span class='op'>=</span> <span class='kw'>false</span>
<span class='kw'>end</span>

<span class='kw'>class</span> <span class='const'>Example</span>
  <span class='kw'>def</span> <span class='id identifier rubyid_foo'>foo</span><span class='lparen'>(</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='label'>bar:</span><span class='rparen'>)</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span>

<span class='id identifier rubyid_example'>example</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
<span class='id identifier rubyid_example'>example</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>a</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>b</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_example'>example</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>a</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>bar:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>b</span><span class='tstring_end'>&#39;</span></span> <span class='rbrace'>}</span><span class='rparen'>)</span>
<span class='comment'># This passes the test, but would result in an ArgumentError in practice</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Strict keyword argument matching (default in Ruby &gt;= 3.0).</p>
</div></h5>
      
      <pre class="example code"><code>
<span class='const'><span class='object_link'><a href="../Mocha.html" title="Mocha (module)">Mocha</a></span></span><span class='period'>.</span><span class='id identifier rubyid_configure'><span class='object_link'><a href="../Mocha.html#configure-class_method" title="Mocha.configure (method)">configure</a></span></span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_c'>c</span><span class='op'>|</span>
  <span class='id identifier rubyid_c'>c</span><span class='period'>.</span><span class='id identifier rubyid_strict_keyword_argument_matching'>strict_keyword_argument_matching</span> <span class='op'>=</span> <span class='kw'>true</span>
<span class='kw'>end</span>

<span class='kw'>class</span> <span class='const'>Example</span>
  <span class='kw'>def</span> <span class='id identifier rubyid_foo'>foo</span><span class='lparen'>(</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='label'>bar:</span><span class='rparen'>)</span><span class='semicolon'>;</span> <span class='kw'>end</span>
<span class='kw'>end</span>

<span class='id identifier rubyid_example'>example</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
<span class='id identifier rubyid_example'>example</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:foo</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>a</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>b</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_example'>example</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>a</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='lbrace'>{</span> <span class='label'>bar:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>b</span><span class='tstring_end'>&#39;</span></span> <span class='rbrace'>}</span><span class='rparen'>)</span>
<span class='comment'># This now fails as expected</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Using a block argument to expect the method to be called with a value divisible by 4.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span> <span class='id identifier rubyid_value'>value</span> <span class='op'>%</span> <span class='int'>4</span> <span class='op'>==</span> <span class='int'>0</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='int'>16</span><span class='rparen'>)</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span> <span class='id identifier rubyid_value'>value</span> <span class='op'>%</span> <span class='int'>4</span> <span class='op'>==</span> <span class='int'>0</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='int'>17</span><span class='rparen'>)</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Extracting a custom matcher into an instance method on the test class.</p>
</div></h5>
      
      <pre class="example code"><code><span class='kw'>class</span> <span class='const'>MyTest</span> <span class='op'>&lt;</span> <span class='const'>Minitest</span><span class='op'>::</span><span class='const'>Test</span>
  <span class='kw'>def</span> <span class='id identifier rubyid_test_expected_method_is_called_with_a_value_divisible_by_4'>test_expected_method_is_called_with_a_value_divisible_by_4</span>
    <span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_method'>method</span><span class='lparen'>(</span><span class='symbol'>:divisible_by_4</span><span class='rparen'>)</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span><span class='lparen'>(</span><span class='int'>16</span><span class='rparen'>)</span>
    <span class='comment'># =&gt; verify succeeds
</span>  <span class='kw'>end</span>

  <span class='id identifier rubyid_private'>private</span>

  <span class='kw'>def</span> <span class='id identifier rubyid_divisible_by_4'>divisible_by_4</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_value'>value</span> <span class='op'>%</span> <span class='int'>4</span> <span class='op'>==</span> <span class='int'>0</span>
  <span class='kw'>end</span>
<span class='kw'>end</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>expected_parameters_or_matchers</span>
      
      
        <span class='type'>(<tt>Array&lt;<span class='object_link'>Object</span>,<span class='object_link'><a href="ParameterMatchers/BaseMethods.html" title="Mocha::ParameterMatchers::BaseMethods (module)">ParameterMatchers::BaseMethods</a></span>&gt;</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>expected parameter values or parameter matchers.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Yields:</p>
<ul class="yield">
  
    <li>
      
      
        <span class='type'></span>
      
      
      
        
        <div class='inline'>
<p>optional block specifying custom matching.</p>
</div>
      
    </li>
  
</ul>
<p class="tag_title">Yield Parameters:</p>
<ul class="yieldparam">
  
    <li>
      
        <span class='name'>actual_parameters</span>
      
      
        <span class='type'>(<tt>Array&lt;<span class='object_link'>Object</span>&gt;</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>parameters with which expected method was invoked.</p>
</div>
      
    </li>
  
</ul>
<p class="tag_title">Yield Returns:</p>
<ul class="yieldreturn">
  
    <li>
      
      
        <span class='type'>(<tt>Boolean</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p><code>true</code> if <code>actual_parameters</code> are acceptable; <code>false</code> otherwise.</p>
</div>
      
    </li>
  
</ul>
<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="ParameterMatchers.html" title="Mocha::ParameterMatchers (module)">ParameterMatchers</a></span></li>
    
      <li><span class='object_link'><a href="Configuration.html#strict_keyword_argument_matching=-instance_method" title="Mocha::Configuration#strict_keyword_argument_matching= (method)">Configuration#strict_keyword_argument_matching=</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


324
325
326
327</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 324</span>

<span class='kw'>def</span> <span class='id identifier rubyid_with'>with</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_expected_parameters_or_matchers'>expected_parameters_or_matchers</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_matching_block'>matching_block</span><span class='rparen'>)</span>
  <span class='ivar'>@parameters_matcher</span> <span class='op'>=</span> <span class='const'>ParametersMatcher</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_expected_parameters_or_matchers'>expected_parameters_or_matchers</span><span class='comma'>,</span> <span class='kw'>self</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_matching_block'>matching_block</span><span class='rparen'>)</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="with_block_given-instance_method">
  
    #<strong>with_block_given</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called with a block.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called with a block.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with_block_given'>with_block_given</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='lbrace'>{</span> <span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with_block_given'>with_block_given</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


344
345
346
347</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 344</span>

<span class='kw'>def</span> <span class='id identifier rubyid_with_block_given'>with_block_given</span>
  <span class='ivar'>@block_matcher</span> <span class='op'>=</span> <span class='const'>BlockMatchers</span><span class='op'>::</span><span class='const'>BlockGiven</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="with_no_block_given-instance_method">
  
    #<strong>with_no_block_given</strong>  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that the expected method must be called without a block.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Expected method must be called without a block.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with_no_block_given'>with_no_block_given</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span>
<span class='comment'># =&gt; verify succeeds
</span>
<span class='id identifier rubyid_object'>object</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:expected_method</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_with_block_given'>with_block_given</span>
<span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_expected_method'>expected_method</span> <span class='lbrace'>{</span> <span class='int'>1</span> <span class='op'>+</span> <span class='int'>1</span> <span class='rbrace'>}</span>
<span class='comment'># =&gt; verify fails</span></code></pre>
    
  </div>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


363
364
365
366</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 363</span>

<span class='kw'>def</span> <span class='id identifier rubyid_with_no_block_given'>with_no_block_given</span>
  <span class='ivar'>@block_matcher</span> <span class='op'>=</span> <span class='const'>BlockMatchers</span><span class='op'>::</span><span class='const'>NoBlockGiven</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
  <span class='kw'>self</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="yields-instance_method">
  
    #<strong>yields</strong>(*parameters)  &#x21d2; <tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>Modifies expectation so that when the expected method is called, it yields to the block with the specified <code>parameters</code>.</p>

<p>If no <code>parameters</code> are specified, it yields to the block without any parameters.</p>

<p>If no block is provided, the method will still attempt to yield resulting in a <code>LocalJumpError</code>. Note that this is what would happen if a “real” (non-mock) method implementation tried to yield to a non-existent block.</p>

<p>May be called multiple times on the same expectation for consecutive invocations.</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h4 class="tag_title">Examples:</h4>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Yield when expected method is invoked.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_benchmark'>benchmark</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_benchmark'>benchmark</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:measure</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_yields'>yields</span>
<span class='id identifier rubyid_yielded'>yielded</span> <span class='op'>=</span> <span class='kw'>false</span>
<span class='id identifier rubyid_benchmark'>benchmark</span><span class='period'>.</span><span class='id identifier rubyid_measure'>measure</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_yielded'>yielded</span> <span class='op'>=</span> <span class='kw'>true</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_yielded'>yielded</span> <span class='comment'># =&gt; true</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Yield parameters when expected method is invoked.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_fibonacci'>fibonacci</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_fibonacci'>fibonacci</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:next_pair</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_yields'>yields</span><span class='lparen'>(</span><span class='int'>0</span><span class='comma'>,</span> <span class='int'>1</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sum'>sum</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='id identifier rubyid_fibonacci'>fibonacci</span><span class='period'>.</span><span class='id identifier rubyid_next_pair'>next_pair</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_first'>first</span><span class='comma'>,</span> <span class='id identifier rubyid_second'>second</span><span class='op'>|</span> <span class='id identifier rubyid_sum'>sum</span> <span class='op'>=</span> <span class='id identifier rubyid_first'>first</span> <span class='op'>+</span> <span class='id identifier rubyid_second'>second</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_sum'>sum</span> <span class='comment'># =&gt; 1</span></code></pre>
    
      
        <h5 class="example_title"><div class='inline'>
<p>Yield different parameters on different invocations of the expected method.</p>
</div></h5>
      
      <pre class="example code"><code><span class='id identifier rubyid_fibonacci'>fibonacci</span> <span class='op'>=</span> <span class='id identifier rubyid_mock'>mock</span><span class='lparen'>(</span><span class='rparen'>)</span>
<span class='id identifier rubyid_fibonacci'>fibonacci</span><span class='period'>.</span><span class='id identifier rubyid_expects'>expects</span><span class='lparen'>(</span><span class='symbol'>:next_pair</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_yields'>yields</span><span class='lparen'>(</span><span class='int'>0</span><span class='comma'>,</span> <span class='int'>1</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_then'>then</span><span class='period'>.</span><span class='id identifier rubyid_yields'>yields</span><span class='lparen'>(</span><span class='int'>1</span><span class='comma'>,</span> <span class='int'>1</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sum'>sum</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='id identifier rubyid_fibonacci'>fibonacci</span><span class='period'>.</span><span class='id identifier rubyid_next_pair'>next_pair</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_first'>first</span><span class='comma'>,</span> <span class='id identifier rubyid_second'>second</span><span class='op'>|</span> <span class='id identifier rubyid_sum'>sum</span> <span class='op'>=</span> <span class='id identifier rubyid_first'>first</span> <span class='op'>+</span> <span class='id identifier rubyid_second'>second</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_sum'>sum</span> <span class='comment'># =&gt; 1
</span><span class='id identifier rubyid_fibonacci'>fibonacci</span><span class='period'>.</span><span class='id identifier rubyid_next_pair'>next_pair</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_first'>first</span><span class='comma'>,</span> <span class='id identifier rubyid_second'>second</span><span class='op'>|</span> <span class='id identifier rubyid_sum'>sum</span> <span class='op'>=</span> <span class='id identifier rubyid_first'>first</span> <span class='op'>+</span> <span class='id identifier rubyid_second'>second</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_sum'>sum</span> <span class='comment'># =&gt; 2</span></code></pre>
    
  </div>
<p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>parameters</span>
      
      
        <span class='type'>(<tt>*Array</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>parameters to be yielded.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Mocha::Expectation (class)">Expectation</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>the same expectation, thereby allowing invocations of other <span class='object_link'><a href="" title="Mocha::Expectation (class)">Mocha::Expectation</a></span> methods to be chained.</p>
</div>
      
    </li>
  
</ul>

  <p class="tag_title">See Also:</p>
  <ul class="see">
    
      <li><span class='object_link'><a href="#then-instance_method" title="Mocha::Expectation#then (method)">#then</a></span></li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


402
403
404</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/mocha/expectation.rb', line 402</span>

<span class='kw'>def</span> <span class='id identifier rubyid_yields'>yields</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_parameters'>parameters</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_multiple_yields'>multiple_yields</span><span class='lparen'>(</span><span class='id identifier rubyid_parameters'>parameters</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>

      <div id="footer">
  Generated on Tue Nov 11 14:27:03 2025 by
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.9.37 (ruby-3.3.0).
</div>

    </div>
  </body>
</html>